package leetcode_回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * 全排列
 * 算法：
 *   回溯
 *   dePath --- 记录当前遍历到第几层了
 */
public class _46 {
    public static void main(String[] args) {
        System.out.println(new _46().permute(new int[]{1, 2, 3}));
    }

    public List<List<Integer>> permute(int[] nums) {
        List<Integer> tem = new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        dfs(nums, tem, ans, used);
        return ans;
    }
    public void dfs(int[] nums, List<Integer> tem, List<List<Integer>> ans, boolean[] used) {
        if (tem.size() == nums.length) {
            ans.add(new ArrayList<>(tem));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!used[i]) {
                tem.add(nums[i]);
                used[i] = true;
                dfs(nums, tem, ans, used);
                used[i] = false;
                tem.remove(tem.size() - 1);
            }
        }
    }
}
